"""
import time

import requests


# 普通方式
def download_img(url):
    print("正在下载：", url)
    response = requests.get(url)
    file_name = url.split('_')[-1]  # 文件名
    with open(file_name, mode='wb') as f:
        f.write(response.content)
    print("下载完成：", url)


if __name__ == '__main__':
    url_list = [
        "https://b.bdstatic.com/searchbox/icms/searchbox/img/young_girl.png",
        "https://b.bdstatic.com/searchbox/icms/searchbox/img/ci_boy.png",
    ]

    for row in url_list:
        time.sleep(0.5)
        download_img(row)
"""
# 协程异步方式
import aiohttp
import asyncio


async def download_img1(session, url):
    print("正在下载：", url)
    async with session.get(url, verify_ssl=False) as response:
        content = await response.content.read()
        file_name = url.split('_')[-1]  # 文件名
        with open(file_name, mode='wb') as f:
            f.write(content)
        print("下载完成：", url)


async def main():
    async with aiohttp.ClientSession() as session:
        print(session)
        url_list = [
            "https://b.bdstatic.com/searchbox/icms/searchbox/img/young_girl.png",
            "https://b.bdstatic.com/searchbox/icms/searchbox/img/ci_boy.png"
        ]
        tasks = [asyncio.create_task(download_img1(session, url)) for url in url_list]  # 创建的多个任务
        print(tasks, "1111")
        await asyncio.wait(tasks)  # 执行


if __name__ == '__main__':
    asyncio.run(main())  # 执行
